package sword_to_1;

public class LeeCode_58_1 {
    public static void main(String[] args) {
//        System.out.println(reverseWords("the sky is blue"));
//        System.out.println(reverseWords("  hello world!  "));
        System.out.println(reverseWords("  "));
    }
    private static String reverseWords(String s) {
        String[] ss = s.split(" ");
        StringBuilder ans = new StringBuilder();
        for(int i = ss.length - 1; i >= 0; i--){
            if (ss[i].length() == 0)
                continue;
            ans.append(ss[i]).append(" ");
        }
        if (ans.length() > 0)
            ans.replace(ans.length() - 1, ans.length(), "");
        return ans.toString();
    }
}
